<template>
  <div>
    <el-drawer
      :title="`${title}投票信息`"
      :visible.sync="drawer"
      direction="rtl"
      size="45%"
      @close="close"
    >
      <el-tabs v-model="activeName">
        <el-tab-pane label="基础信息" name="repairs">
          <el-form :model="formData" label-position="top">
            <el-row>
              <el-col :span="8">
                <el-form-item label="所属小区">
                  <SelectView v-model="formData.community_code" />
                </el-form-item>
              </el-col>
              <el-col :span="2">
                <el-form-item label="" />
              </el-col>
              <el-col :span="8">
                <el-form-item label="投票标题">
                  <el-input v-model="formData.title" size="small" placeholder="请输入" />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="8">
                <el-form-item label="活动开始时间" prop="begin_time">
                  <el-date-picker
                    v-model="formData.start_time"
                    size="small"
                    value-format="timestamp"
                    type="datetime"
                    placeholder="选择日期时间"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="2">
                <el-form-item label="" />
              </el-col>
              <el-col :span="8">
                <el-form-item label="活动结束时间" prop="end_time">
                  <el-date-picker
                    v-model="formData.end_time"
                    size="small"
                    value-format="timestamp"
                    type="datetime"
                    placeholder="选择日期时间"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="是否支持多选" prop="begin_time">
                  <div>
                    <el-radio v-model="formData.multi" label="0">不支持</el-radio>
                    <el-radio v-model="formData.multi" label="1">支持</el-radio>
                  </div>
                </el-form-item>
              </el-col>
              <el-col :span="2">
                <el-form-item label="" />
              </el-col>
              <el-col :span="8">
                <el-form-item label="是否匿名" prop="end_time">
                  <div>
                    <el-radio v-model="formData.anonymous" label="0">匿名</el-radio>
                    <el-radio v-model="formData.anonymous" label="1">不匿名</el-radio>
                  </div>
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item label="状态" prop="end_time">
              <div>
                <el-radio v-model="formData.status" label="0">关闭</el-radio>
                <el-radio v-model="formData.status" label="1">启用</el-radio>
              </div>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="btnOk">提交</el-button>
              <el-button>取消</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <!-- <span>===============================</span> -->
        <el-tab-pane label="选项信息" name="first">
          <el-table
            style="width: 100%"
            :data="formData.options"
          >
            <el-table-column
              prop="name"
              label="选项名称"
              width="180"
            >
              <template scope="{row}">
                <el-input v-model="row.name" placeholder="请输入" />
              </template>
            </el-table-column>
            <el-table-column
              prop="name"
              label="选项描述"
              width="180"
            >
              <template scope="{row}">
                <el-input v-model="row.description" type="textarea" placeholder="请输入" />
              </template>
            </el-table-column>
            <el-table-column
              prop="address"
              label="图片地址"
            >
              <template scope="{row}">
                <UploadImage v-model="row.img_url" />
              </template>
            </el-table-column>
            <el-table-column
              prop="address"
              label="操作"
            >
              <template scope="{row}">
                <el-button type="text" @click="del(row.id,row.indexId)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="add" @click="add">+ 新增选项</div>
          <el-button type="primary" @click="btnOk">提交</el-button>
          <el-button>取消</el-button>
        </el-tab-pane>
        <!-- <span>===============================</span> -->
        <el-tab-pane label="详细信息" name="reply">
          <el-form ref="form" :rules="formRules" :model="formData" label-position="top">
            <el-row>
              <el-col :span="8">
                <el-form-item label="备注" prop="status">
                  <el-input v-model="formData.title" :autosize="{ minRows: 4, maxRows: 40}" type="textarea" size="small" placeholder="请输入" />
                </el-form-item>
              </el-col>
              <el-col :span="2">
                <el-form-item label="" />
              </el-col>
              <el-col :span="8">
                <el-form-item label="回复图片">
                  <UploadImage v-model="formData.img_url" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item label="回复内容" prop="reply">
              <!-- 富文本编辑器 -->
              <quill-editor v-model="formData.description" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="btnOk">提交</el-button>
              <el-button>取消</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>

    </el-drawer>
  </div>
</template>

<script>
// require styles
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import SelectView from './selectView'
import UploadImage from './upload-image'
import { quillEditor } from 'vue-quill-editor'
import { echoTicketAPI, editTicketAPI, addTicketAPI } from '@/api/hujingbo/ticket'
export default {
  name: 'RepairDrawer',
  components: {
    SelectView,
    UploadImage,
    quillEditor
  },
  data() {
    return {
      drawer: false,
      activeName: 'repairs',
      formData: {
        options: [],
        cstart_time: '',
        start_time: '',
        title: '',
        status: '',
        remark: '',
        multi: '',
        img_url: '',
        end_time: '',
        description: '',
        community_code: '',
        anonymous: ''
      },
      formRules: {
      }
    }
  },
  computed: {
    title() {
      return this.formData.id ? '修改' : '添加'
    }
  },
  methods: {
    // 回显
    async echoRepair(id) {
      const res = await echoTicketAPI(id)
      this.formData = res.data
      this.formData.status = String(res.data.status)
      this.formData.anonymous = String(res.data.anonymous)
      this.formData.multi = String(res.data.multi)
    },
    // 提交
    async btnOk() {
      await this.$refs.form.validate()
      this.formData.multi = Number(this.formData.multi)
      this.formData.anonymous = Number(this.formData.anonymous)
      this.formData.status = Number(this.formData.status)
      this.formData.options = this.formData.options.filter(item => {
        return item.description || item.img_url || item.name
      })
      if (this.formData.id) {
        await editTicketAPI(this.formData)
      } else {
        // const {} = this.formData
        await addTicketAPI(this.formData)
      }
      this.$emit('btnOk')
      this.drawer = false
      this.$message.success('操作成功')
    },
    // 添加信息
    add() {
      if (this.formData.options.length >= 9) return this.$message.warning('不可以在添加数据啦')
      this.formData.options.push({ indexId: Date.parse(new Date()) })
    },
    del(id, index) {
      console.log(id, index)
      this.formData.options = this.formData.options.filter(item => {
        console.log(item)
        if (id) {
          return item.id !== id
        } else {
          return item.indexId !== index
        }
      })
    },
    close() {
      this.formData = {
        options: [],
        cstart_time: '',
        start_time: '',
        title: '',
        status: '',
        remark: '',
        multi: '',
        img_url: '',
        end_time: '',
        description: '',
        community_code: '',
        anonymous: ''
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-tabs{
    padding:0 30px;
}
  ::v-deep .ql-editor {
    height: 300px;
  }
.add{
  margin-top: 20px;
  width: 100%;
 height: 60px;
 text-align: center;
 line-height: 60px;
 border: 1px dashed #e2e2e2;
}
</style>
